var layer;
var $;
var element;
var form;
var laydate;
var table;
layui.use(['element','layer','laydate','form','table'], function(){
    layer = layui.layer;
    $ = layui.jquery;
    element = layui.element;
    form = layui.form;
    laydate = layui.laydate;
    table = layui.table;
    load();
    selIndexUserTab();
})
/**
 *  点击事件  刷新  返回
 * */
function load(){
    $(".tit_btn").bind('click',function(){
        location.reload();
    })
    $(".tit_btn2").bind('click',function(){
        window.history.go(-1);  //返回上一页
    })
    $("#indexBtn").bind('click',function(){
        selIndexUserTab();
        layer.msg("搜索成功", {icon: 1});
    })
}
/**
 *  封号用户列表
 */
function selIndexUserTab(obj){
    var userId = $("input[name=userId]").val();
    var reason = $("input[name=reason]").val();
    var days = $("select[name=days]").val();
    var type = $("select[name=type]").val();
    if(!userId){
        obj+="&userId="+0;
    }else{
        obj+="&userId="+userId;
    }
    if(!reason){
        obj+="&reason="+'';
    }else{
        obj+="&reason="+reason;
    }
    if(!days){
        obj+="&days="+0;
    }else{
        obj+="&days="+days;
    }
    if(!type){
        obj+="&type="+0;
    }else{
        obj+="&type="+type;
    }
    $.ajax({
        url:"/personnel/selClosureUserTable",
        type:"post",
        dataType:"json",
        data:obj,
        success:function(item) {
            userTab(item);
        }
    })
}
function userTab(item){
    table.render({
        elem:"#userListTab",//容器
        title:"封号用户数据列表",
        limit:10,//分页显示条数
        width:1223,
        data:item,
        toolbar:true,
        page:{//自定义分页
            layout:['count','prev','page','next','limit','skip','refresh'],
            limits:[3,6,9],//分页选项
            first:"首页",
            last:"尾页",
        },
        cols:[[//定义列
            {type:'checkbox',align:'center',width:60},//复选框靠左
            {title:'序号',align:'center',templet:function(data){
                    return data.LAY_INDEX;
                }},
            {field:"userId",title:"用户编号",align:'center',width:120},
            {field:"startDate",title:"封停时间",align:'center',width:190},
            {field:"endDate",title:"解封时间",align:'center',width:190},
            {field:"reason",title:"封停原因",align:'center',width:200},
            {field:"days",title:"封停天数",align:'center',width:120,templet:function(data){
                if(data.days==0){
                    return "已解封";
                }else{
                    return data.days+"天";
                }
             }},
            {field:"",title:"用户",align:'center',width:120,templet:function(data){
                    if(data.type==2){
                        return "前端用户";
                    }else if(data.type==3){
                        return "管理端人员";
                    }
                }},
            {field:"",title:"操作",align:'center',width:140,templet:function(data){
                    return "<a style='color:#009688;cursor:pointer;' onclick='checkswitchon(\""+data.userId+"\",\""+data.reason+"\",\""+data.id+"\",\""+data.days+"\",\""+data.type+"\")'>解封</a> " +
                        "<a style='color:#009688;cursor:pointer;' onclick='delUser(\""+data.userId+"\",\""+data.id+"\",\""+data.days+"\")'>删除</a>" ;
                }}
        ]]
    });
}
/**
 *  删除用户
 */
function delUser(userId,id,days){
    if(days!=0){
        layer.msg("该账号目前处于封禁状态，无法删除");
    }else{
        $.ajax({
            url:"/personnel/selUserInfo",
            type:"post",
            dataType:"json",
            data:{"userId":userId},
            success:function(item) {
                layer.confirm("确定删除`"+item.account+"`账户的封停日志？",{btn: ['确定', '取消'],title:"提示"}, function(){
                    var tablename = "用户封停数据列表";
                    var type= 3;
                    var remark = id+"";
                    addOperLog(tablename,type,remark);
                    $.ajax({
                        url:"/personnel/delBeforeStatus",
                        type:"post",
                        dataType:"json",
                        data:{"id":id},
                        success:function(data){
                            if(data){
                                layer.msg("删除成功", {icon: 1});
                                selIndexUserTab();
                            }else{
                                layer.msg("删除失败", {icon: 2});
                                selIndexUserTab();
                            }
                        }
                    })
                });
            }
        })
    }
}
/**
 *  管理启用开关
 */
function checkswitchon(userId,reason,id,days,type){
    if(days==0){
        layer.msg("该账户已被解封")
    }else{
        layer.open({
            type:2,//弹出层的类型
            title:"解封用户",
            area:['550px','400px'],//弹出层大小
            shade:0,//设置遮罩
            offset:'auto',//设置遮罩的位置
            maxmin:true,
            //弹出层内容
            content :"/personnel/layer/PerUserList_layer_checkswitchon.html",
            success:function(layero,index){
                if(type==2){
                    $.ajax({
                        url:"/personnel/selUserInfo",
                        type:"post",
                        dataType:"json",
                        data:{"userId":userId},
                        success:function(item) {
                            //查找当前引入的iframe的内容
                            var obj = layero.find('iframe').contents();
                            obj.find("input[name=userId]").val(userId);
                            obj.find("input[name=reason]").val(reason);
                            obj.find("input[name=account]").val(item.account);
                            obj.find("input[name=nickname]").val(item.nickname);
                            obj.find("input[name=id]").val(id);
                        }
                    })
                }else if(type==3){
                    $.post("/personnel/selBackUserInfo", {userId:userId}, function (item) {
                        //查找当前引入的iframe的内容
                        var obj = layero.find('iframe').contents();
                        obj.find("input[name=userId]").val(userId);
                        obj.find("input[name=reason]").val(reason);
                        obj.find("input[name=account]").val(item.account);
                        obj.find("input[name=nickname]").val(item.trueName);
                        obj.find("input[name=id]").val(id);
                    });
                }
            },
            btn:['确定','取消'],
            yes:function(index,layero){
                var body = layero.find('iframe').contents();
                var remark = body.find("textarea[name=reason]").val();
                var ty = 3;
                addClosureLogOn(userId,ty,remark);
                var form = $(body).find('form').serialize();
                form+="&type="+type;
                updClosure(form);
                layer.close(layer.index);
            },btn2:function(index,layero){

            }
        })
    }
}
function updClosure(vals){
    $.ajax({
        url:"/personnel/updClosure",
        type:"post",
        dataType:"json",
        data:vals,
        success:function(data){
            if(data){
                layer.msg("解封成功", {icon: 1});
                selIndexUserTab();
            }else{
                layer.msg("解封失败", {icon: 2});
                selIndexUserTab();
            }
        }
    })
}
/**
 *  添加后台操作日志
 * */
function addOperLog(tablename,type,remark){
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    var mid = create.userId;
    form+="&mid="+mid;
    form+="&tablename="+tablename;
    form+="&type="+type;
    form+="&remark="+remark;
    $.post("/personnel/addOperationLog", form, function (res) {
        form="";
    });
}
/**
 *  添加解封操作日志
 * */
function addClosureLogOn(userId,type,remark){
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    var operationId = create.userId;
    form+="&operationId="+operationId;
    form+="&userId="+userId;
    form+="&type="+type;
    form+="&remark="+remark;
    $.post("/personnel/addClosureLog", form, function (res) {
        form="";
    });
}